import * as types from './mutation_types'
import request from '../../../util/axios'

// 获取电子文件表头
export const getTableHead = ({ commit }, tableId) => {
  commit(types.TABLEHEAD_SET_RETRIEVED, null)
  commit(types.SORT_SET_RETRIEVED, null)
  commit(types.TABLELIST_SET_LOADING, true)
  return request({
    url: `/yuyi-category-boot/api/metadata/user/sort/${tableId}`
  }).then((resSort) => {
    commit(types.SORT_SET_RETRIEVED, resSort.data.data)
    return request({
      url: `/yuyi-category-boot/api/metadata/user/tableHead/${tableId}`,
      method: 'get',
      data: {}
    }).then((res) => {
      res.data.data.map(items => {
        items.sort = false
        resSort.data.data.map(item => {
          if (items.mdCode === item.mdCode) {
            items.sort = true
          }
        })
      })
      commit(types.TABLEHEAD_SET_RETRIEVED, res.data.data)
      return res
    }).catch(e => {
      commit(types.TABLEHEAD_SET_RETRIEVED, null)
      return e
    })
  }).catch(e => {
    commit(types.SORT_SET_RETRIEVED, null)
    console.log(e)
  })
}

// 获取电子文件列表
export const getTableList = ({ commit }, val) => {
  commit(types.TABLELIST_SET_RETRIEVED, null)
  commit(types.TABLELIST_SET_LOADING, true)

  return request({
    url: `/yuyi-category-boot/api/arc/data/${val.idTable}/list`,
    method: 'post',
    data: val.data
  }).then((res) => {
    commit(types.TABLELIST_SET_RETRIEVED, res.data.data)
    commit(types.TABLELIST_SET_LOADING, false)
  }).catch(e => {
    commit(types.TABLELIST_SET_RETRIEVED, null)
    commit(types.TABLELIST_SET_LOADING, false)
  })
}
